import React, { useContext } from 'react';
import { Alert, Skeleton } from 'antd';
import { useRequest, defaultFormatResult } from '@/hooks';
import {
    getEmployeeBankCardInfo,
    getEmployeeSalaryInfo,
    getEmployeeInsuranceAndProvidentInfo,
    saveEmployeeBankCardInfo,
    saveEmployeeSalaryInfo,
    saveEmployeeInsuranceAndProvidentInfo,
    getPersonalTaxSpecialDeduction,
    savePersonalTaxSpecialDeduction,
    getCpf,
    saveCpf
} from '@/pages/Organization/EmployeeManage/services';
import BankCardSection from '@/pages/Organization/EmployeeManage/components/EmployeeInfoSections/BankCardSection';
import SalarySection from '@/pages/Organization/EmployeeManage/components/EmployeeInfoSections/SalarySection';
import InsuranceProvidentSection from '@/pages/Organization/EmployeeManage/components/EmployeeInfoSections/InsuranceProvidentSection';
import PersonalTaxSpecialDeductionSection from '@/pages/Organization/EmployeeManage/components/EmployeeInfoSections/PersonalTaxSpecialDeductionSection';
import CpfSection from '@/pages/Organization/EmployeeManage/components/EmployeeInfoSections/CpfSection';
import SalaryRefSection from '@/pages/Organization/EmployeeManage/components/EmployeeInfoSections/SalaryRefSection';
import EmployeeDetailContext from '@/pages/Organization/EmployeeManage/EmployeeDetail/store/context';
import authHelper from '@/utils/auth/helper';

/**
 * 员工详情 - 工资社保
 */
const SalaryTabContent = ({ employeeCode, employeeInfoData }) => {
    const employeeDetailContext = useContext(EmployeeDetailContext);
    const currentEmployeeCountryCode = employeeDetailContext?.employeeInfo?.countryNameCode;
    const singaporeIdType = employeeDetailContext?.singaporeIdType;
    // 员工工资卡数据
    const { data: banCardData, loading: banCardLoading, error: banCardError, mutate: banCardMutate } = useRequest(
        () => getEmployeeBankCardInfo({ employeeCode }),
        {
            cacheKey: `employee-ban-card-${employeeCode}`,
            refreshDeps: [employeeCode],
            ...defaultFormatResult
        }
    );
    // 员工工资数据
    const { data: salaryData, loading: salaryLoading, error: salaryError, run: getSalary } = useRequest(
        () => getEmployeeSalaryInfo({ employeeCode }),
        {
            cacheKey: `employee-salary-info-${employeeCode}`,
            refreshDeps: [employeeCode],
            ...defaultFormatResult
        }
    );
    // 员工社保公积金数据
    const {
        data: insuranceProvidentData,
        loading: insuranceProvidentLoading,
        error: insuranceProvidentError,
        mutate: insuranceProvidentMutate
    } = useRequest(() => getEmployeeInsuranceAndProvidentInfo({ employeeCode }), {
        cacheKey: `employee-insurance-provident-${employeeCode}`,
        refreshDeps: [employeeCode],
        ...defaultFormatResult
    });

    // 员工社保公积金数据
    const {
        data: personalTaxSpecialDeductionData,
        loading: personalTaxSpecialDeductionLoading,
        error: personalTaxSpecialDeductionError,
        mutate: personalTaxSpecialDeductionMutate
    } = useRequest(() => getPersonalTaxSpecialDeduction({ employeeCode }), {
        cacheKey: `employee-personal-tax-special-deduction-${employeeCode}`,
        refreshDeps: [employeeCode],
        ...defaultFormatResult
    });

    // cpf
    const { data: cpfData, loading: cpfLoading, error: cpfError, mutate: cpfMutate } = useRequest(
        () => getCpf({ employeeCode }),
        {
            cacheKey: `employee-cpf-${employeeCode}`,
            refreshDeps: [employeeCode],
            ...defaultFormatResult
        }
    );

    // 错误处理
    if (salaryError || banCardError || insuranceProvidentError || personalTaxSpecialDeductionError || cpfError) {
        return (
            <Alert
                type="error"
                message={
                    salaryError?.msg ||
                    banCardError?.msg ||
                    insuranceProvidentError?.msg ||
                    personalTaxSpecialDeductionError?.msg ||
                    cpfError?.msg ||
                    '数据加载错误'
                }
            />
        );
    }

    // render
    return (
        <>
            <Skeleton loading={!banCardData || banCardLoading} active>
                {/* 员工工资 */}
                <BankCardSection
                    formData={banCardData}
                    onChange={changedData => banCardMutate(oldDta => ({ ...oldDta, ...changedData }))}
                    // readonly={
                    //     // !authHelper.checkCode('office:orgs:employee:detail:getSalary:update')
                    //     // !isPersonalPage
                    // }
                    saveAction={saveEmployeeBankCardInfo}
                />
            </Skeleton>
            <Skeleton loading={!salaryData || salaryLoading} active>
                {/* 员工工资卡 */}
                <SalarySection
                    formData={salaryData}
                    readonly={!authHelper.checkCode('office:orgs:employee:detail:updateSalary') || employeeInfoData?.serviceStatus === 6}
                    saveAction={saveEmployeeSalaryInfo}
                    onChange={getSalary}
                    employeeCode={employeeCode}
                />
            </Skeleton>
            <Skeleton loading={false} active>
                <SalaryRefSection salaryData={salaryData} />
            </Skeleton>
            {currentEmployeeCountryCode === 'CN' && (
                <Skeleton loading={!insuranceProvidentData || insuranceProvidentLoading} active>
                    {/* 员工社保公积金 */}
                    <InsuranceProvidentSection
                        formData={insuranceProvidentData}
                        onChange={changedData => insuranceProvidentMutate(oldDta => ({ ...oldDta, ...changedData }))}
                        // readonly={!isPersonalPage}
                        saveAction={saveEmployeeInsuranceAndProvidentInfo}
                    />
                </Skeleton>
            )}
            {currentEmployeeCountryCode === 'CN' && (
                <Skeleton loading={!personalTaxSpecialDeductionData || personalTaxSpecialDeductionLoading} active>
                    {/* 个税专项扣除 */}
                    <PersonalTaxSpecialDeductionSection
                        formData={personalTaxSpecialDeductionData}
                        onChange={changedData =>
                            personalTaxSpecialDeductionMutate(oldDta => ({ ...oldDta, ...changedData }))
                        }
                        // readonly={!isPersonalPage}
                        saveAction={savePersonalTaxSpecialDeduction}
                    />
                </Skeleton>
            )}
            {currentEmployeeCountryCode === 'SG' && singaporeIdType === '1' && (
                <Skeleton loading={!cpfData || cpfLoading} active>
                    {/* CPF */}
                    <CpfSection
                        formData={cpfData}
                        onChange={changedData => cpfMutate(oldDta => ({ ...oldDta, ...changedData }))}
                        // readonly={!isPersonalPage}
                        saveAction={saveCpf}
                    />
                </Skeleton>
            )}
        </>
    );
};

export default SalaryTabContent;
